<script type="text/javascript">
	var url;
	function newUser(){
		$('#dlg').dialog('open').dialog('setTitle','添加用户');
		$('#fm').form('clear');
		url = '<?php echo url('user','add')?>';
	}
	
	function saveNew(){
		$('#fm').form('submit',{
			url: url,
			onSubmit: function(){
				return $(this).form('validate');
			},
			success: function(result){
				var result = eval('('+result+')');
				if (result.success){
					$('#dlg').dialog('close');		// close the dialog
					$('#dg').datagrid('reload');	// reload the user data
				} else {
					$.messager.show({
						title: '错误',
						msg: result.msg
					});
				}
			}
		});
	}
	
	function editUser(){
		var row = $('#dg').datagrid('getSelected');
		if (row){
			$('#dlg2').dialog('open').dialog('setTitle','编辑用户');
			$('#fm2').form('load',row);
			url = '<?php echo url('user','update')?>&id='+row.id;
		}
	}
	function saveEdit(){
		$('#fm2').form('submit',{
			url: url,
			onSubmit: function(){
				return $(this).form('validate');
			},
			success: function(result){
				var result = eval('('+result+')');
				if (result.success){
					$('#dlg2').dialog('close');		// close the dialog
					$('#dg').datagrid('reload');	// reload the user data
				} else {
					$.messager.show({
						title: '错误',
						msg: result.msg
					});
				}
			}
		});
	}
	function removeUser(){
		var row = $('#dg').datagrid('getSelected');
		if (row){
			$.messager.confirm('确认','一定要删除该用户吗?',function(r){
				if (r){
					$.post('<?php echo url('user','del')?>',{id:row.id},function(result){
						if (result.success){
							$('#dg').datagrid('reload');	// reload the user data
						} else {
							$.messager.show({	// show error message
								title: '错误',
								msg: result.msg
							});
						}
					},'json');
				}
			});
		}
	}
</script>

<table id="dg" title="用户管理" class="easyui-datagrid"
		url="<?php echo url('user','list')?>"
		toolbar="#toolbar" pagination="true"
		rownumbers="true" fitColumns="true" fit="true" singleSelect="true">
	<thead>
		<tr>
			<th field="sirname" width="30">姓</th>
			<th field="givenname" width="50">名</th>
			<th field="username" width="100">帐号</th>
			<th field="dept" width="50">部门</th>
			<th field="persona_id" width="80">身份证</th>
			<th field="tel" width="50">电话</th>
			<th field="mob" width="50">手机</th>
			<th field="last_login_at" width="80">上次登录时间</th>
			<th field="last_login_ip" width="50">上次登录地址</th>
			<th field="is_operator" width="30">操作权限</th>
			<th field="is_locked" width="30">锁定</th>
		</tr>
	</thead>
</table>
<div id="toolbar">
	<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">添加用户</a>
	<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">编辑用户</a>
	<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeUser()">删除用户</a>
</div>

<div id="dlg" class="easyui-dialog" style="width:400px;height:420px;padding:10px 20px"
		closed="true" buttons="#dlg-buttons">
	<div class="ftitle">用户信息</div>
	<form id="fm" method="post" novalidate>
		<div class="fitem">
			<label>帐号:</label>
			<input name="username" class="easyui-validatebox" required="true">
		</div>
		<div class="fitem">
			<label>密码:</label>
			<input name="new-password" class="easyui-validatebox" required="true" validType="minLength[6]">
		</div>
		<div class="fitem">
			<label>姓:</label>
			<input name="sirname" class="easyui-validatebox" required="true">
		</div>
		<div class="fitem">
			<label>名:</label>
			<input name="givenname" class="easyui-validatebox" required="true">
		</div>
		<div class="fitem">
			<label>部门:</label>
			<select class="easyui-combotree" url="<?php echo url('dept','combotree')?>" name="dept_id" style="width:156px;"/>
			<input name="dept" type="hidden">
		</div>
		<div class="fitem">
			<label>身份证:</label>
			<input name="persona_id">
		</div>
		<div class="fitem">
			<label>电话:</label>
			<input name="tel">
		</div>
		<div class="fitem">
			<label>手机:</label>
			<input name="mob">
		</div>
		<div class="fitem">
			<label>操作权限:</label>
			<input name="is_operator" type="checkbox" value="1">
		</div>
	</form>
</div>
<div id="dlg-buttons">
	<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveNew()">保存</a>
	<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
</div>

<div id="dlg2" class="easyui-dialog" style="width:400px;height:400px;padding:10px 20px"
		closed="true" buttons="#dlg-buttons2">
	<div class="ftitle">用户信息</div>
	<form id="fm2" method="post" novalidate>
		<div class="fitem">
			<label>帐号:</label>
			<input name="username" readonly>
		</div>
		<div class="fitem">
			<label>密码:</label>
			<input name="new-password" class="easyui-validatebox" validType="minLength[6]">
		</div>
		<div class="fitem">
			<label>姓:</label>
			<input name="sirname" class="easyui-validatebox" required="true">
		</div>
		<div class="fitem">
			<label>名:</label>
			<input name="givenname" class="easyui-validatebox" required="true">
		</div>
		<div class="fitem">
			<label>部门:</label>
			<select class="easyui-combotree" url="<?php echo url('dept','combotree')?>" name="dept_id" style="width:156px;"/>
			<input name="dept" type="hidden">
		</div>
		<div class="fitem">
			<label>身份证:</label>
			<input name="persona_id">
		</div>
		<div class="fitem">
			<label>电话:</label>
			<input name="tel">
		</div>
		<div class="fitem">
			<label>手机:</label>
			<input name="mob">
		</div>
		<div class="fitem">
			<label>操作权限:</label>
			<input name="is_operator" type="checkbox" value="1">
		</div>
		<div class="fitem">
			<label>锁定:</label>
			<input name="is_locked" type="checkbox" value="1">
		</div>
	</form>
</div>
<div id="dlg-buttons2">
	<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveEdit()">保存</a>
	<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg2').dialog('close')">取消</a>
</div>